package com.collect.cameroon.zhiScene.zhiDsChangeWallet.zhiFollow;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiAdater.zhiDs.ZHIDsChangeWalletListAdjuster;
import com.collect.cameroon.zhiBase.BaseFragment;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRDsChangeWalletEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRDsChangeWalletInvalidMobilesEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRDsChangeWalletSmsContentEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISDSChangeWalletSmsEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISDSGetChangeWalletInfoEntity;
import com.collect.cameroon.zhiGlobal.zhiAppSettings.ZHIProjectConfigs;
import com.collect.cameroon.zhiHelper.ZHIDsHelper;
import com.collect.cameroon.zhiHelper.ZHISysShareHelper;
import com.collect.cameroon.zhiUtils.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIDsChangeWalletFollowFragment extends BaseFragment<ZHIDsChangeWalletFollowPresenter> implements ZHIDsChangeWalletFollowContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.rc_list)
    RecyclerView rc_list;

    @BindView(R.id.tv_mobile_search)
    TextView tv_mobile_search;
    @BindView(R.id.edit_wallet_change_search)
    EditText edit_wallet_change_search;

    @BindView(R.id.tv_wallet_change_product)
    TextView tv_wallet_change_product;
    @BindView(R.id.tv_wallet_change_amount)
    TextView tv_wallet_change_amount;
    @BindView(R.id.tv_wallet_change_days)
    TextView tv_wallet_change_days;
    @BindView(R.id.tv_wallet_change_repayment_amount)
    TextView tv_wallet_change_repayment_amount;
    @BindView(R.id.tv_wallet_change_sms_times)
    TextView tv_wallet_change_sms_times;
    @BindView(R.id.ll_wallet_change_mobile)
    LinearLayout ll_wallet_change_mobile;
    @BindView(R.id.tv_wallet_change_mobile)
    TextView tv_wallet_change_mobile;
    @BindView(R.id.ll_wallet_change_send_sms)
    LinearLayout ll_wallet_change_send_sms;
    @BindView(R.id.ll_wallet_change_send_whatsapp)
    LinearLayout ll_wallet_change_send_whatsapp;
    @BindView(R.id.ll_wallet_change_cancel_order)
    LinearLayout ll_wallet_change_cancel_order;

    private final ZHIDsChangeWalletListAdjuster mZHIDsChangeWalletListAdjuster = new ZHIDsChangeWalletListAdjuster();

    private String mOrderId = "";

    private final ArrayList<ZHIRDsChangeWalletInvalidMobilesEntity> mOrderList = new ArrayList<>();

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_follow_ds_change_wallet;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));
            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onFetchChangeWalletUserInfo(ZHIRDsChangeWalletEntity zhirDsChangeWalletEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            edit_wallet_change_search.setText(null);

            mOrderId = zhirDsChangeWalletEntity.getOrderId();
            tv_wallet_change_product.setText(zhirDsChangeWalletEntity.getProduct());
            tv_wallet_change_amount.setText(zhirDsChangeWalletEntity.getAmount());
            tv_wallet_change_days.setText(zhirDsChangeWalletEntity.getDays());
            tv_wallet_change_repayment_amount.setText(zhirDsChangeWalletEntity.getRepayAmount());
            tv_wallet_change_sms_times.setText(zhirDsChangeWalletEntity.getTodaySendSmsTimes());
            tv_wallet_change_mobile.setText(zhirDsChangeWalletEntity.getMobile());

            mOrderList.clear();
            mOrderList.addAll(zhirDsChangeWalletEntity.getInvalidWalletMobiles());
            mZHIDsChangeWalletListAdjuster.setNewData(mOrderList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSendChangeWalletUserSms(ZHIRDsChangeWalletSmsContentEntity zhirDsChangeWalletSmsContentEntity) {
        ZHIDsHelper.LAST_DS_CALL_TIME = System.currentTimeMillis();
        if (TextUtils.isEmpty(zhirDsChangeWalletSmsContentEntity.getContent())) {
            initData(true);
        } else {
            //电话号码
            String mobile = tv_wallet_change_mobile.getText().toString().trim();

            //内容
            String content = zhirDsChangeWalletSmsContentEntity.getContent();

            //复制内容
            ZHISysShareHelper.copyToClipboard(requireActivity(), content);

            //发送whatsapp
            ZHISysShareHelper.sendMsgWithWhatsApp(requireActivity(), ZHIProjectConfigs.PRE_PHONE + mobile, content);
        }
    }

    @Override
    public void onCancelApplyOrder() {
        initData(true);
    }

    @SuppressLint({"InflateParams", "NotifyDataSetChanged"})
    public void initEvent() {
        try {
            srl_swipe_layout.setOnRefreshListener(() -> initData(false));
            mZHIDsChangeWalletListAdjuster.setEmptyView(getRefreshEmptyView());
            rc_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_list.setAdapter(mZHIDsChangeWalletListAdjuster);

            //搜索
            tv_mobile_search.setOnClickListener(view -> {
                initData(true);
            });

            //打电话
            ll_wallet_change_mobile.setOnClickListener(view -> {
                //电话号码
                String mobile = tv_wallet_change_mobile.getText().toString().trim();

                //电话操作
                ZHISysShareHelper.callPhone(getActivity(), mobile, (mobile.length() <= 9));
            });

            //发短信
            ll_wallet_change_send_sms.setOnClickListener(view -> {
                //电话号码
                String mobile = tv_wallet_change_mobile.getText().toString().trim();

                //判断是否已打电话
                if (!checkCallResult(mobile)) {
                    return;
                }

                //标记
                if (mPresenter != null) {
                    ZHISDSChangeWalletSmsEntity zhisdsChangeWalletSmsEntity = new ZHISDSChangeWalletSmsEntity();
                    zhisdsChangeWalletSmsEntity.setOrderId(mOrderId);
                    zhisdsChangeWalletSmsEntity.setTypeSms();
                    mPresenter.sendChangeWalletUserSms(getActivity(), zhisdsChangeWalletSmsEntity, true);
                }
            });

            //发whatsapp
            ll_wallet_change_send_whatsapp.setOnClickListener(view -> {
                //电话号码
                String mobile = tv_wallet_change_mobile.getText().toString().trim();

                //判断是否已打电话
                if (!checkCallResult(mobile)) {
                    return;
                }

                //标记
                if (mPresenter != null) {
                    ZHISDSChangeWalletSmsEntity zhisdsChangeWalletSmsEntity = new ZHISDSChangeWalletSmsEntity();
                    zhisdsChangeWalletSmsEntity.setOrderId(mOrderId);
                    zhisdsChangeWalletSmsEntity.setTypeWhatsApp();
                    mPresenter.sendChangeWalletUserSms(getActivity(), zhisdsChangeWalletSmsEntity, true);
                }
            });

            //取消借款
            ll_wallet_change_cancel_order.setOnClickListener(view -> {
                //标记
                if (mPresenter != null) {
                    ZHISDSChangeWalletSmsEntity zhisdsChangeWalletSmsEntity = new ZHISDSChangeWalletSmsEntity();
                    zhisdsChangeWalletSmsEntity.setOrderId(mOrderId);
                    mPresenter.cancelApplyOrder(getActivity(), zhisdsChangeWalletSmsEntity, true);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initData(boolean showLoading) {
        if (mPresenter != null) {
            String mobile = edit_wallet_change_search.getText().toString().trim();
            mPresenter.fetchChangeWalletUserInfo(getActivity(), new ZHISDSGetChangeWalletInfoEntity(mobile), showLoading);
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> {
            initData(true);
        });
        return emptyView;
    }

    private boolean checkCallResult(String mobile) {
        int result = ZHIDsHelper.getInstance().checkCallRecordForChangeWallet(mobile);
        if (ZHIDsHelper.RESULT_CALL_SUCCESS == result) {
            return true;
        }
        ToastUtil.s("Est-ce vraiment le cas ? S'il vous plaît, prenez le travail au sérieux-" + result);
        return false;
    }

    public void refreshData() {
        initData(true);
    }


}